package com.StarStudio.Controller;

import com.StarStudio.Service.CouponsStockService;
import com.StarStudio.Utils.QueryPageUtils;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.Entity.*;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/coupons_stock")
@Api(tags = "优惠券库存接口")
public class CouponsStockController {

    @Autowired
    CouponsStockService couponsStockService;

    /**
     * 优惠券库存分页方法
     * @param couponsStock 优惠券库存对象
     * @return 分页结果对象 PageResult
     */
    @GetMapping("/page")
    @ApiOperation("优惠券库存分页接口 -> 支持条件检索，检索条件封装在优惠券库存个体对象CouponsStock中。如需分页，必传pageNum和pageSize")
    public PageResult Page(CouponsStock couponsStock){
        QueryPageUtils.startPage();
        PageInfo pageInfo;
        long allRecordNumber;
        List<CouponsStock> couponsStockList;
        try {
            couponsStockList = couponsStockService.findCouponsStockList(couponsStock);
            allRecordNumber = couponsStockService.findAllRecordNumber();
            pageInfo = new PageInfo<>(couponsStockList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.QUERY_PAGE_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS,MessageConstant.QUERY_PAGE_SUCCESS, pageInfo.getTotal(), couponsStockList, allRecordNumber);
    }

    /**
     * 导出优惠券库存详情信息到 Excel表格
     * @param response http响应
     * @param couponsStock 优惠券库存对象
     */
    @Log(title = "优惠券库存详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("优惠券库存Excel导出接口 -> 支持条件检索，检索条件封装在优惠券库存个体对象CouponsStock中")
    public void export(HttpServletResponse response, CouponsStock couponsStock) {
        List<CouponsStock> list = couponsStockService.findCouponsStockList(couponsStock);
        ExcelUtil<CouponsStock> util = new ExcelUtil<>(CouponsStock.class);
        util.exportExcel(response, list, "优惠券库存数据");
    }


}
